package dao;

import entity.MembersInfo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MembersInfoDao extends BaseDao {
    public List<MembersInfo> getAllMembers() throws SQLException {
        List<MembersInfo> members = new ArrayList<>();
        String sql = "SELECT * FROM MEMBERSINFO";
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = getConnection().prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                MembersInfo member = new MembersInfo();
                member.setMid(rs.getInt("MID"));
                member.setMname(rs.getString("MNAME"));
                member.setMgender(rs.getString("MGENDER"));
                member.setMage(rs.getInt("MAGE"));
                member.setMaddress(rs.getString("MADDRESS"));
                member.setMemail(rs.getString("MEMAIL"));

                members.add(member);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(getConnection(), pstmt, rs);
        }

        return members;
    }

    public MembersInfo getMemberById(int id) throws SQLException {
        String sql = "SELECT * FROM MEMBERSINFO WHERE MID = ?";
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = getConnection().prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                MembersInfo member = new MembersInfo();
                member.setMid(rs.getInt("MID"));
                member.setMname(rs.getString("MNAME"));
                member.setMgender(rs.getString("MGENDER"));
                member.setMage(rs.getInt("MAGE"));
                member.setMaddress(rs.getString("MADDRESS"));
                member.setMemail(rs.getString("MEMAIL"));

                return member;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(getConnection(), pstmt, rs);
        }

        return null;
    }

    public boolean updateMember(MembersInfo member) throws SQLException {
        String sql = "UPDATE MEMBERSINFO SET MNAME = ?, MGENDER = ?, MAGE = ?, MADDRESS = ?, MEMAIL = ? WHERE MID = ?";
        PreparedStatement pstmt = null;

        try {
            pstmt = getConnection().prepareStatement(sql);
            pstmt.setString(1, member.getMname());
            pstmt.setString(2, member.getMgender());
            pstmt.setInt(3, member.getMage());
            pstmt.setString(4, member.getMaddress());
            pstmt.setString(5, member.getMemail());
            pstmt.setInt(6, member.getMid());

            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(getConnection(), pstmt, null);
        }

        return false;
    }

    public boolean deleteMember(int id) throws SQLException {
        String sql = "DELETE FROM MEMBERSINFO WHERE MID = ?";
        PreparedStatement pstmt = null;

        try {
            pstmt = getConnection().prepareStatement(sql);
            pstmt.setInt(1, id);

            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(getConnection(), pstmt, null);
        }

        return false;
    }
}
